<?php
set_time_limit(0);
error_reporting(E_ALL);
require_once './Classes/PHPExcel.php';
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', '日期')
            ->setCellValue('B1', '浏览量(PV)')
            ->setCellValue('C1', '访客数(UV)')
            ->setCellValue('D1', 'IP数')
            ->setCellValue('E1', 'TAG')
;
include 'c.php';
$index=2;

$day=date("Y-m-d");
$tag='all';
if (isset($_GET['tag'])){
    $tag=$_GET['tag'];
}


$mysqli = new mysqli($dbhost, $dbuser, $dbpwd, $dbName);
if (!$mysqli->set_charset("utf8")) {printf("Error loading character set utf8: %s\n", $mysqli->error);}

if (true){
    $thetag=$tag;
    if ($thetag != 'all'){
        $sql="SELECT count(id) as count FROM analyse where day='".$day."' and  tag='".$thetag."' ";
    }else{
        $sql="SELECT count(id) as count FROM analyse where day='".$day."' ";
    }
    $queryPv=$mysqli->query($sql);
    $pvInfo=$queryPv->fetch_array();
    
    if ($thetag != 'all'){
        $sql="SELECT count(DISTINCT uid) as count FROM analyse where day='".$day."' and  tag='".$thetag."'  ";
    }else{
        $sql="SELECT count(DISTINCT uid) as count FROM analyse where day='".$day."'   ";
    }
    $queryUv=$mysqli->query($sql);
    $uvInfo=$queryUv->fetch_array();
    
    if ($thetag != 'all'){
        $sql="SELECT count(DISTINCT ip) as count FROM analyse where day='".$day."' and  tag='".$thetag."'  ";
    }else{
        $sql="SELECT count(DISTINCT ip) as count FROM analyse where day='".$day."'   ";
    }
    $queryIp=$mysqli->query($sql);
    $ipInfo=$queryIp->fetch_array();
    $outData[]=array('day'=>$day, 'pv'=>$pvInfo['count'], 'uv'=> $uvInfo['count'],
        'ip'=>$ipInfo['count'],'tag'=>$thetag
    );
    foreach ($outData as $list){
        $objPHPExcel->setActiveSheetIndex(0)
        ->setCellValue('A'.$index, $list['day'])
        ->setCellValue('B'.$index, $list['pv'])
        ->setCellValue('C'.$index, $list['uv'])
        ->setCellValue('D'.$index, $list['ip'])
        ->setCellValue('E'.$index, $list['tag'])
        ;
        $index++;
    }
    $sql="SELECT * FROM log_2 where tag='".$thetag."' order by id desc ";
    
    $query=$mysqli->query($sql);
    while ($list=$query->fetch_assoc()){
        $objPHPExcel->setActiveSheetIndex(0)
        ->setCellValue('A'.$index, $list['day'])
        ->setCellValue('B'.$index, $list['pv'])
        ->setCellValue('C'.$index, $list['uv'])
        ->setCellValue('D'.$index, $list['ip'])
        ->setCellValue('E'.$index, $list['tag'])
        ;
        $index++;
    }
    
    
    
}else{
    $query=$mysqli->query("SELECT tag FROM analyse group by tag ");
    $tagName=array();
    while ($list=$query->fetch_assoc()){
        $tagName[]=$list['tag'];
    }
    $outData=array();
    foreach ($tagName as $thetag){
        if ($thetag != 'all'){
            $sql="SELECT count(id) as count FROM analyse where day='".$day."' and  tag='".$thetag."' ";
        }else{
            $sql="SELECT count(id) as count FROM analyse where day='".$day."' ";
        }
        $queryPv=$mysqli->query($sql);
        $pvInfo=$queryPv->fetch_array();
        if ($thetag != 'all'){
            $sql="SELECT count(DISTINCT uid) as count FROM analyse where day='".$day."' and  tag='".$thetag."'  ";
        }else{
            $sql="SELECT count(DISTINCT uid) as count FROM analyse where day='".$day."'   ";
        }
        $queryUv=$mysqli->query($sql);
        $uvInfo=$queryUv->fetch_array();
        
        if ($thetag != 'all'){
            $sql="SELECT count(DISTINCT ip) as count FROM analyse where day='".$day."' and  tag='".$thetag."'  ";
        }else{
            $sql="SELECT count(DISTINCT ip) as count FROM analyse where day='".$day."'   ";
        }
        $queryIp=$mysqli->query($sql);
        $ipInfo=$queryIp->fetch_array();
        
        
        $outData[]=array('day'=>$day, 'pv'=>$pvInfo['count'], 'uv'=> $uvInfo['count'],
            'ip'=>$ipInfo['count'],'tag'=>$thetag
        );
    }
    
    foreach ($outData as $list){
        $objPHPExcel->setActiveSheetIndex(0)
        ->setCellValue('A'.$index, $list['day'])
        ->setCellValue('B'.$index, $list['pv'])
        ->setCellValue('C'.$index, $list['uv'])
        ->setCellValue('D'.$index, $list['ip'])
        ->setCellValue('E'.$index, $list['tag'])
        ;
        $index++;
    }
    
    $sql="SELECT * FROM log_2 order by id desc ";
    
    $query=$mysqli->query($sql);
    while ($list=$query->fetch_assoc()){
        $objPHPExcel->setActiveSheetIndex(0)
        ->setCellValue('A'.$index, $list['day'])
        ->setCellValue('B'.$index, $list['pv'])
        ->setCellValue('C'.$index, $list['uv'])
        ->setCellValue('D'.$index, $list['ip'])
        ->setCellValue('E'.$index, $list['tag'])
        ;
        $index++;
    }
}





 
// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('用户数据');

// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);


// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="用户数据.xlsx"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');

// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
